//import com.google.gson.Gson
//import org.apache.iotdb.isession.template.Template
//import org.apache.iotdb.isession.template.TemplateNode
//import org.apache.iotdb.session.Session
//import org.apache.iotdb.session.template.MeasurementNode
//import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType
//import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType
//import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding
//import org.apache.iotdb.tsfile.read.common.RowRecord
//
//
//sealed class DataProperty (
//    val id:String,
//    val name:String,
//    val ip:String,
//    val port:Int,
//    val username:String,
//    val password:String
//)
//
//interface DataController {
//    fun open(t: DataProperty): Boolean
//    fun close(): Boolean
//    fun remove(dStr: String): Boolean
//    fun create(cStr:String): Boolean
//    fun alter(cStr: String): Boolean
//    fun drop(cStr: String): Boolean
//    fun insert(cStr: String): Any?
//    fun update(cStr: String): Any?
//    fun delete(cStr: String): Boolean
//    fun query(cStr: String): List<Any>?
//}
//
//interface IotDataController: DataController {
//    fun createTimeSeries(path: String, type:TSDataType, encoding:TSEncoding, compressType:CompressionType) : Boolean
//    fun deleteTimeSeries(path: String)
//    fun createMultiTimeSeries(pathList:MutableList<String>, typeList: MutableList<TSDataType>, codingList:MutableList<TSEncoding>, compressList:MutableList<CompressionType>): Boolean
//    fun createAlignedTimeSeries(device:String, measurements:MutableList<String>, typeList: MutableList<TSDataType>, codingList:MutableList<TSEncoding>, compressList:MutableList<CompressionType>): Boolean
//    fun createIotSchemaTemplate(templateName:String, nameList:MutableList<String>, typeList: MutableList<TSDataType>, codingList: MutableList<TSEncoding>, compressList: MutableList<CompressionType>)
//    fun setSchemaTemplate(templateName:String, prefixPath:String)
//    fun showIotTemplates()
//}
//
//class MeasurementNode(name: String, dataType: TSDataType, encoding: TSEncoding, compressionType: CompressionType) : TemplateNode(name){
//    private val dataType: TSDataType? = null
//    private val encoding: TSEncoding? = null
//    private val compressionType: CompressionType? = null
//}
//
//class IotProperty(id: String, name: String, ip: String = "127.0.0.1", port: Int = 6667, username: String = "root", password: String = "root") :
//    DataProperty(id, name, ip, port, username, password) {
//    var fetchSize:Int = 1024
//    var version:String = "1.0"
//    var enableRPC = false
//
//    fun config(fs: Int, v:String, rpc:Boolean){
//        fetchSize = fs
//        version = v
//        enableRPC = rpc
//    }
//}
//
//data class IotLowerEntity (
//    val edgeIns: EdgeComputing,
//    val meters: List<SmartMeter>,
//    val sockets: List<SmartSocket>
//)
//
//data class IotUpperEntity (
//    val cloudIns:CloudComputing,
//    val brokerIns: MessageBroker,
//    val children: MutableList<IotLowerEntity>
//)
//
//class IotMaster (dp: DataProperty): IotDataController {
//
//    lateinit var iotSession: Session
//    val iotDataProperty: DataProperty
//
//    init {
//        iotDataProperty = dp
//        try {
//            iotSession = Session.Builder().build()
//            val newProperty = dp as IotProperty
//            iotSession.open(newProperty.enableRPC)
//        }catch (e: Exception){
//            println(e.toString())
//        }
//    }
//
//    fun showDataBase() {
//        iotSession.executeQueryStatement("show databases root.**")
//    }
//
//    override fun createTimeSeries(
//        path: String,
//        type: TSDataType,
//        encoding: TSEncoding,
//        compressType: CompressionType
//    ): Boolean {
//        if(!iotSession.checkTimeseriesExists(path)) iotSession.createTimeseries(path, type, encoding, compressType)
//        return true
//    }
//
//    fun showTimeSeries(): MutableList<RowRecord>{
//        val result:MutableList<RowRecord> = mutableListOf()
//        val dataSets = iotSession.executeQueryStatement("show timeseries root.**")
//        dataSets.fetchSize = 1024
//        while (dataSets.hasNext()){
//            result.add(dataSets.next())
//        }
//        return result
//    }
//
//    override fun deleteTimeSeries(path: String) {
//        iotSession.deleteTimeseries(path)
//    }
//
//    override fun createMultiTimeSeries(
//        pathList: MutableList<String>,
//        typeList: MutableList<TSDataType>,
//        codingList: MutableList<TSEncoding>,
//        compressList: MutableList<CompressionType>
//    ): Boolean {
//        pathList.forEach {
//            if(iotSession.checkTimeseriesExists(it)) return false
//        }
//        try {
//            iotSession.createMultiTimeseries(pathList, typeList, codingList, compressList, null, null, null, null)
//
//        }catch (e: Exception){
//            println(e.toString())
//            return false
//        }
//        return true
//    }
//
//    override fun createAlignedTimeSeries(
//        device: String,
//        measurements: MutableList<String>,
//        typeList: MutableList<TSDataType>,
//        codingList: MutableList<TSEncoding>,
//        compressList: MutableList<CompressionType>
//    ): Boolean {
//        if(iotSession.checkTimeseriesExists(device)) return false
//        try {
//            iotSession.createAlignedTimeseries(device, measurements, typeList, codingList, compressList, null, null, null)
//        }catch (e: Exception){
//            println(e.toString())
//            return false
//        }
//        return true
//    }
//
//    override fun createIotSchemaTemplate(templateName:String, nameList:MutableList<String>, typeList: MutableList<TSDataType>, codingList: MutableList<TSEncoding>, compressList: MutableList<CompressionType>) {
//        val template = Template(templateName)
//        nameList.forEachIndexed { index, s ->
//            val node = MeasurementNode(s, typeList[index], codingList[index], compressList[index])
//            template.addToTemplate(node)
//        }
//        iotSession.createSchemaTemplate(template)
//    }
//
//    override fun setSchemaTemplate(templateName:String, prefixPath:String) {
//        iotSession.setSchemaTemplate(templateName, prefixPath)
//    }
//
//    fun unsetSchemaTemplate(templateName:String, prefixPath:String) {
//        iotSession.unsetSchemaTemplate(prefixPath, templateName)
//    }
//
//    fun dropSchemaTemplate(templateName: String){
//        iotSession.dropSchemaTemplate(templateName)
//    }
//
//    override fun showIotTemplates() {
//        TODO("Not yet implemented")
//    }
//
//    override fun open(t: DataProperty): Boolean {
//        TODO("Not yet implemented")
//    }
//
//    override fun close(): Boolean {
//        try {
//            iotSession.close()
//        }catch (e: Exception){
//            println(e.toString())
//            return false
//        }
//        return true
//    }
//
//    fun initDb(dStr: String): Boolean {
//        val result:MutableList<RowRecord> = mutableListOf()
//        try {
////            val dataSets = iotSession.executeQueryStatement("show databases root.*")
////            dataSets.fetchSize = 1024
////            while (dataSets.hasNext()){
////                result.add(dataSets.next())
////            }
////            result.forEach {
////                if (it.fields[0].stringValue == dStr) return false
////            }
//            iotSession.createDatabase(dStr)
//        }catch (e: Exception){
//            println(e.toString())
//            return false
//        }
//        return true
//    }
//
//    fun deleteDb (dStr: String) {
//        iotSession.deleteDatabase(dStr)
//    }
//
//    fun showTemplates(): MutableList<String> {
//        return iotSession.showAllTemplates()
//    }
//
//    fun showTemplatesPath(templateName: String): MutableList<String> {
//        return iotSession.showPathsTemplateSetOn(templateName)
//    }
//
//    override fun remove(dStr: String): Boolean {
//        try {
//            iotSession.deleteDatabase(dStr)
//        }catch (e: Exception){
//            println(e.toString())
//            return false
//        }
//        return true
//    }
//
//    override fun create(cStr: String): Boolean {
//        TODO("Not yet implemented")
//    }
//
//    override fun alter(cStr: String): Boolean {
//        TODO("Not yet implemented")
//    }
//
//    override fun drop(cStr: String): Boolean {
//        try {
//            if(!iotSession.checkTimeseriesExists(cStr)) iotSession.deleteTimeseries(cStr)
//        }catch (e: Exception){
//            println(e.toString())
//            return false
//        }
//        return true
//    }
//
//    override fun insert(cStr: String): Any? {
//        TODO("Not yet implemented")
//    }
//
//    override fun update(cStr: String): Any? {
//        TODO("Not yet implemented")
//    }
//
//    override fun delete(cStr: String): Boolean {
//        TODO("Not yet implemented")
//    }
//
//    override fun query(cStr: String): List<Any>? {
//        TODO("Not yet implemented")
//    }
//
//    fun initOfMeta() {
//        val infoString = UtilFuncs.readFile("src/main/resources/logic.json")
//        val infoEntity:IotUpperEntity = Gson().fromJson(infoString, IotUpperEntity::class.java)
//        val logicSets = infoEntity.children
//        logicSets.forEach { lower ->
//            val (gateway, meters, sockets) = lower // 数据类的解构声明
////            iotSession.createDatabase("root.D${gateway.id}") //创建数据库
////            iotSession.setSchemaTemplate("Sck_20", "root.D${gateway.id}.Sck_20") // 创建sck元数据
////            iotSession.setSchemaTemplate("Trm_at", "root.D${gateway.id}.Trm_at") // 创建trm元数据
//            meters.forEach { meter ->
//                val path = "root.D${gateway.id}.Trm_at.${meter.id.replace("-","_")}" //得到了path，创建时间序列
//                println("create timeseries using schema template on $path")
//                iotSession.executeNonQueryStatement("create timeseries using schema template on $path")
////                iotSession.createTimeseries(path, TSDataType.FLOAT, TSEncoding.RLE, CompressionType.SNAPPY)
//            }
//            sockets.forEach { socket ->
//                val path = "root.D${gateway.id}.Sck_20.${socket.id.replace("-","_")}"
////                iotSession.createTimeseries(path, TSDataType.FLOAT, TSEncoding.RLE, CompressionType.SNAPPY)
//            }
//        }
//        iotSession.executeQueryStatement("show timeseries root.**")
//            .use { dataSet ->
//                println(dataSet.getColumnNames())
//                dataSet.setFetchSize(1024) // default is 10000
//                while (dataSet.hasNext()) {
//                    println(dataSet.next())
//                }
//            }
//    }
//}

//fun main(args: Array<String>) {
//
//    val linkProp = IotProperty("Database-${UtilFuncs.unique(8,1)}","zh-redmi-iotdb")
//    val iotdb = IotMaster(linkProp as DataProperty)
//
//    /** --------------
//    val templateName = "Sck_20"
//    val nameList = mutableListOf("U", "I", "P", "Energy")
//    val typeList: MutableList<TSDataType> = mutableListOf()
//    val codingList: MutableList<TSEncoding> = mutableListOf()
//    val compressList: MutableList<CompressionType> = mutableListOf()
//    for(i in 0 until 4){
//    typeList.add(TSDataType.FLOAT)
//    codingList.add(TSEncoding.RLE)
//    compressList.add(CompressionType.SNAPPY)
//    }
//    iotdb.createIotSchemaTemplate(templateName, nameList, typeList, codingList, compressList)
//    iotdb.setSchemaTemplate("Sck_20", "root.D12345678.Sck_20")
//
//    val templateName = "Trm_at"
//    val nameList = mutableListOf("U", "I", "P", "Q", "f", "PF", "Energy")
//    val typeList: MutableList<TSDataType> = mutableListOf()
//    val codingList: MutableList<TSEncoding> = mutableListOf()
//    val compressList: MutableList<CompressionType> = mutableListOf()
//    for(i in 0 until 7){
//    typeList.add(TSDataType.FLOAT)
//    codingList.add(TSEncoding.RLE)
//    compressList.add(CompressionType.SNAPPY)
//    }
//    iotdb.createIotSchemaTemplate(templateName, nameList, typeList, codingList, compressList)
//
//    iotdb.setSchemaTemplate("Trm_at", "root.D12345678.Trm_at")
//
//    注意 template创建必须在没有timeseries的条件下进行
//    先 iotdb.setSchemaTemplate("Sck_20", "root.D12345678")
//    再 创建 iotdb.createTimeSeries("root.D12345678.Sck_20.Sck_test", TSDataType.FLOAT, TSEncoding.RLE, CompressionType.SNAPPY)
//
//    # 清除templates
//    iotdb.showTemplates().forEach {
//        iotdb.dropSchemaTemplate(it)
//    }
//
//    -----------------------   **/
//
//
////    iotdb.initDb("root.D12345678")
////    iotdb.createTimeSeries("root.D12345678.Sck_20.Sck_test", TSDataType.FLOAT, TSEncoding.RLE, CompressionType.SNAPPY)
//
//
////    iotdb.deleteTimeSeries("root.D12345678.Sck_20.Sck_test")
////    iotdb.setSchemaTemplate("Sck_20", "root.D12345678")
//
//
////    val db = iotdb.init("root.test")
////    val db = iotdb.remove("root.test")
//
////    iotdb.initOfMeta()
//
////    iotdb.dropSchemaTemplate("Sck_20")
////    iotdb.dropSchemaTemplate("Trm_at")
//
////    iotdb.unsetSchemaTemplate("Trm_at", "root.D86570000003.Trm_at")
//
////    val series = iotdb.showTimeSeries()
////    series.forEach {
////        iotdb.deleteTimeSeries(it.fields[0].toString())
////    }
//
//    repeat(3) {
//        println("aaa")
//    }
//
////    iotdb.showTemplatesPath("Trm_at").forEach {
////        println(it)
////    }
//
////    iotdb.createTimeSeries("root.D12345678.Sck_21.Sck_test", TSDataType.FLOAT, TSEncoding.RLE, CompressionType.SNAPPY)
//
//
//    val isClosed = iotdb.close()
//}